package com.zero.lite.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zero.lite.admin.annotation.LoginAdmin;
import com.zero.lite.admin.service.AdminTokenManager;
import com.zero.lite.admin.util.bcrypt.BCryptPasswordEncoder;
import com.zero.lite.dao.entity.Ad;
import com.zero.lite.dao.entity.Admin;
import com.zero.lite.dao.entity.Menu;
import com.zero.lite.dao.service.IAdminService;

import com.zero.lite.dao.service.IMenuService;
import com.zero.lite.dao.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IMenuService menuService;

    @GetMapping("/info")
    public Object info(String token){
        Integer adminId = AdminTokenManager.getUserId(token);
        if(adminId == null){
            return ResponseUtil.badArgumentValue();
        }
        Admin admin = adminService.selectById(adminId);
        if(admin == null){
            return ResponseUtil.badArgumentValue();
        }

        Map<String, Object> data = new HashMap<>();
        data.put("name", admin.getUsername());
        data.put("avatar", admin.getAvatar());

        //查询用户菜单
        List<Menu> menus=adminService.queryAllMenus(adminId);
        data.put("menus",menus);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       String username,
                       Page page){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        EntityWrapper<Admin> ew=new EntityWrapper<>();
        ew.eq("user_name",username);
        Page<Admin> data=adminService.selectPage(page,ew);

        return ResponseUtil.PageOk(data);
    }

    @PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody Admin admin){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        String rawPassword = admin.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(rawPassword);
        admin.setPassword(encodedPassword);

        adminService.insert(admin);
        return ResponseUtil.ok(admin);
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin Integer adminId, Integer id){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        if(id == null){
            return ResponseUtil.badArgument();
        }

        Admin admin = adminService.selectById(id);
        return ResponseUtil.ok(admin);
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody Admin admin){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        Integer anotherAdminId = admin.getId();
        if(anotherAdminId.intValue() == 1){
            return ResponseUtil.fail(403, "超级管理员不能修改");
        }

        adminService.updateById(admin);
        return ResponseUtil.ok(admin);
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody Admin admin){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        Integer anotherAdminId = admin.getId();
        if(anotherAdminId.intValue() == 1){
            return ResponseUtil.fail(403, "超级管理员不能删除");
        }
        adminService.deleteById(anotherAdminId);
        return ResponseUtil.ok();
    }


    @GetMapping("/menu/list")
    public Object listMenu(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        List<Menu> menus=adminService.queryAllMenus(adminId);
        List<Map> rs=new ArrayList<>();
        for (Menu m:menus){
            Map p=new HashMap();
            if (m.getChildren()!=null){
                List<Map> children=new ArrayList<>();
                for (Menu mm:m.getChildren()) {
                    Map c=new HashMap();
                    if (mm.getAuth()!=null){
                        List<Map> auth=new ArrayList<>();
                        for (Menu mmm:mm.getAuth()) {
                            Map a=new HashMap();
                            a.put("id",mmm.getMenuId());
                            a.put("label",mmm.getTitle());
                            auth.add(a);
                        }
                        c.put("children",auth);
                    }
                    c.put("id",mm.getMenuId());
                    c.put("label",mm.getTitle());
                    children.add(c);
                }
                p.put("children",children);
            }
            p.put("id",m.getMenuId());
            p.put("label",m.getTitle());
            rs.add(p);
        }
        return ResponseUtil.ok(rs);
    }

    @PostMapping("/menu/create")
    public Object createMenu(@LoginAdmin Integer adminId, @RequestBody Menu menu){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        menuService.insert(menu);
        return ResponseUtil.ok(menu);
    }

    @GetMapping("/menu/read")
    public Object readMenu(@LoginAdmin Integer adminId, Integer menuId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        if(menuId == null){
            return ResponseUtil.badArgument();
        }

        Menu menu = menuService.selectById(menuId);
        return ResponseUtil.ok(menu);
    }

    @PostMapping("/menu/update")
    public Object updateMenu(@LoginAdmin Integer adminId, @RequestBody Menu menu){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        menuService.updateById(menu);
        return ResponseUtil.ok(menu);
    }

    @PostMapping("/menu/delete")
    public Object deleteMenu(@LoginAdmin Integer adminId, Integer menuId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        if(menuId == null){
            return ResponseUtil.badArgument();
        }
        menuService.deleteById(menuId);
        return ResponseUtil.ok();
    }
}
